﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace CloseDisplay
{
    public partial class Form1 : Form
    {
        LockScreenIdleMonitor monitor = new LockScreenIdleMonitor();


        public Form1()
        {
            InitializeComponent();

            string path = Application.ExecutablePath;
            if (GetRegistry(path) != null)
            {
                checkBox1.Checked = true;
            }


            monitor.IdlTick += (s, sec) =>
            {
                richTextBox1.AppendText($"{DateTime.Now:HH:mm:ss}  键鼠闲置 {sec} 秒\n");
                richTextBox1.ScrollToCaret();

                if (richTextBox1.Lines.Count() > 100)
                {
                    richTextBox1.Text = "";
                }
            };

            monitor.IdlHandle += (s, e) =>
            {
                richTextBox1.AppendText($"{DateTime.Now:HH:mm:ss}  键鼠达到{monitor.Threshold}秒未操作\n");
                richTextBox1.ScrollToCaret();
                Tools.TurnOffMonitor();//关闭显示器

                if (richTextBox1.Lines.Count() > 100)
                {
                    richTextBox1.Text = "";
                }
            };

            //monitor.Threshold = 15;
            //monitor.Start();
        }

        #region 开机自启

        private void checkBox1_CheckedChanged(object sender, EventArgs e)//设置开机是否自启动
        {
            string path = Application.ExecutablePath;
            if (checkBox1.Checked)
            {
                SetRegistry(1, path);
            }
            else
            {
                SetRegistry(0, path);
            }
        }

        /// <summary>  
        /// 开机启动: 设置注册表中的键值  
        /// </summary>  
        /// <param name="flag">1:开机启动</param>  
        public static void SetRegistry(int flag, string path)
        {
            string keyName = path.Substring(path.LastIndexOf("\\") + 1);
            Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (flag == 1)
            {
                if (Rkey == null)
                {
                    Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                }
                Rkey.SetValue(keyName, path);
            }
            else
            {
                if (Rkey != null)
                {
                    Rkey.DeleteValue(keyName, false);
                }
            }
            Rkey.Close();
        }

        /// <summary>  
        /// 读取注册表中的键值  
        /// </summary>  
        /// <param name="flag">1:开机启动</param>  
        public static object GetRegistry(string path)
        {
            string keyName = path.Substring(path.LastIndexOf("\\") + 1);
            Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (Rkey == null)
                return null;
            var obj = Rkey.GetValue(keyName);
            if (obj == null)
                return null;
            Rkey.Close();
            return obj;
        }
        #endregion

        #region 角标

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            WindowState = FormWindowState.Normal;
            this.Focus();
            this.TopMost = true;
            this.TopMost = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.notifyIcon1.Visible = true;
                this.Hide();
                return;
            }
            else//非用户操作
            {
                e.Cancel = false;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

        private void 主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            WindowState = FormWindowState.Normal;
            this.Focus();
            this.TopMost = true;
            this.TopMost = false;
        }
        #endregion
    }
}
